package com.example.zyfypt_611cgt.model;

import com.example.zyfypt_611cgt.bean.LoginBean;
import com.example.zyfypt_611cgt.common.Common;
import com.example.zyfypt_611cgt.iface.LoginListener;
import com.example.zyfypt_611cgt.iface.Loginiface;
import com.example.zyfypt_611cgt.iface.RegisterListener;
import com.example.zyfypt_611cgt.iface.Registeriface;
import com.example.zyfypt_611cgt.service.UserService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RegisterModel implements Registeriface{
        private Retrofit retrofit;
        //构造函数
        public RegisterModel()
        {//使用Retrofit----1
            retrofit=new Retrofit.Builder()
                .baseUrl(Common.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        }

    @Override
    public void getRegisterResult(String username, String pass, String tel, RegisterListener registerListener) {
        //使用Retrofit----2
        UserService userService = retrofit.create(UserService.class);
        Call<String> call = userService.register(username,pass,tel);
        //使用Retrofit----3
        call.enqueue(new Callback<String>(){
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful() && response != null) {
                    registerListener.onResponse(response.body());
                }
                else
                    registerListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                registerListener.onFail(t.toString());
            }
        });
    }
}